var t = require;
var e = module;
var o = exports;
var a,
    i =
        (this && this.__extends) ||
        ((a = function (t, e) {
            return (a =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (t, e) {
                        t.__proto__ = e;
                    }) ||
                function (t, e) {
                    for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
                })(t, e);
        }),
        function (t, e) {
            a(t, e);
            function o() {
                this.constructor = t;
            }
            t.prototype = null === e ? Object.create(e) : ((o.prototype = e.prototype), new o());
        }),
    n =
        (this && this.__decorate) ||
        function (t, e, o, a) {
            var i,
                n = arguments.length,
                r = n < 3 ? e : null === a ? (a = Object.getOwnPropertyDescriptor(e, o)) : a;
            if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(t, e, o, a);
            else
                for (var s = t.length - 1; s >= 0; s--)
                    (i = t[s]) && (r = (n < 3 ? i(r) : n > 3 ? i(e, o, r) : i(e, o)) || r);
            return n > 3 && r && Object.defineProperty(e, o, r), r;
        };
Object.defineProperty(o, "__esModule", {value: !0});
var r = cc._decorator,
    s = r.ccclass,
    c = r.property,
    p =
        (r.menu,
        (function (t) {
            i(e, t);
            function e() {
                var e = (null !== t && t.apply(this, arguments)) || this;
                e.basePreFab = null;
                e.dragdir_v = !0;
                e.makeDir_v = !0;
                e.isIgnoreChildenClick = !0;
                e.baseItemDis = 0;
                e.startItemOffect = 0;
                e.spaceDis = 0;
                e.curPreItemCount = 0;
                e.baseScrInfo = {
                    OY: 0,
                    itemMaxCount: 0,
                    hfDis: 0,
                    limitMin: 0,
                    limitMax: 0,
                    curMin: 0,
                    curMax: 0,
                    PageHeight: 0,
                    viewContent: null,
                    oldViewConY: 0,
                    tempY: 0,
                    OldCur: 0,
                    NowCur: 0,
                    TempItem: null,
                    eqkind: 0
                };
                e.toolsInfo = {pooler: [], all: [], setItemAction: null, IsCheckInfo: !1, IsCheckScrillEevent: !1};
                e.oldRemberPos = null;
                return e;
            }
            e.prototype.onLoad = function () {};
            e.prototype.reSetBaseInfo = function (t, e, o) {
                if (this.checKIsEnterMapSelect()) {
                    this.recAll();
                    this.baseScrInfo.viewContent ||
                        (this.baseScrInfo.viewContent = this.makeScroll(
                            "scroll",
                            this.dragdir_v
                                ? cc.v2(0, -this.node.height * this.node.anchorY)
                                : cc.v2(-this.node.width * this.node.anchorX, 0),
                            cc.v2(this.node.width, this.node.height)
                        ));
                    if (this.baseScrInfo.viewContent) {
                        var a = this.dragdir_v ? this.node.height : this.node.width;
                        this.baseScrInfo.itemMaxCount = Math.ceil(a / (this.baseItemDis + this.spaceDis)) + 1;
                        this.baseScrInfo.hfDis += 0.5 * (this.baseItemDis + this.spaceDis);
                        this.baseScrInfo.PageHeight =
                            this.baseScrInfo.itemMaxCount * (this.baseItemDis + this.spaceDis);
                        if (o) {
                            this.toolsInfo.setItemAction = o;
                            this.baseScrInfo.limitMin = 0;
                            this.baseScrInfo.limitMax = t - this.baseScrInfo.itemMaxCount;
                            for (var i = 0; i < this.baseScrInfo.itemMaxCount && i < t; i++) {
                                var n = this.getNewItem();
                                o(n.id, n.v, n.c);
                                this.setItemPos(n.v, i);
                            }
                            this.dragdir_v
                                ? (this.baseScrInfo.viewContent.height =
                                      (this.baseItemDis + this.spaceDis) * t + this.startItemOffect)
                                : (this.baseScrInfo.viewContent.width =
                                      (this.baseItemDis + this.spaceDis) * t + this.startItemOffect);
                            this._basePos(e);
                            this.oldRemberPos || (this.oldRemberPos = this.baseScrInfo.viewContent.position);
                        } else console.log("设置回调不能为空");
                    }
                } else console.log("预制体信息未填写");
            };
            e.prototype.moveToLevel = function (t, e, o) {
                t >= this.baseScrInfo.limitMin &&
                    t < this.baseScrInfo.limitMax &&
                    this._moveLevelDis(t - this.baseScrInfo.curMin, e, o);
            };
            e.prototype._moveLevelDis = function (t, e, o) {
                var a = this;
                if (0 != t) {
                    this.baseScrInfo.viewContent.runAction(
                        cc.sequence([
                            cc.moveBy(e, this.getOffectMoveV2(t)),
                            cc.callFunc(function () {
                                o && o();
                                a.toolsInfo.IsCheckScrillEevent = !1;
                            }, this)
                        ])
                    );
                    this.toolsInfo.IsCheckScrillEevent = !0;
                    this.toolsInfo.IsCheckInfo = !0;
                }
            };
            e.prototype.scrollEvent = function () {
                this.makeDir_v
                    ? (this.baseScrInfo.tempY =
                          (this.dragdir_v ? this.baseScrInfo.viewContent.y : this.baseScrInfo.viewContent.x) -
                          this.baseScrInfo.OY)
                    : (this.baseScrInfo.tempY =
                          (this.dragdir_v ? -this.baseScrInfo.viewContent.y : -this.baseScrInfo.viewContent.x) -
                          this.baseScrInfo.OY);
                this.baseScrInfo.tempY = Math.floor(this.baseScrInfo.tempY / (this.baseItemDis + this.spaceDis));
                if (this.baseScrInfo.tempY != this.baseScrInfo.oldViewConY) {
                    this.baseScrInfo.tempY < this.baseScrInfo.limitMin &&
                        (this.baseScrInfo.tempY = this.baseScrInfo.limitMin);
                    this.baseScrInfo.tempY > this.baseScrInfo.limitMax &&
                        (this.baseScrInfo.tempY = this.baseScrInfo.limitMax);
                    this.toolsInfo.IsCheckInfo = !0;
                }
            };
            e.prototype.update = function () {
                this.toolsInfo.IsCheckScrillEevent && this.scrollEvent(null, null);
                if (this.toolsInfo.IsCheckInfo) {
                    if (this.baseScrInfo.oldViewConY == this.baseScrInfo.tempY) {
                        this.toolsInfo.IsCheckInfo = !1;
                        return;
                    }
                    if (this.baseScrInfo.tempY > this.baseScrInfo.oldViewConY) {
                        this.baseScrInfo.oldViewConY++;
                        this.baseScrInfo.eqkind = 2;
                    } else if (this.baseScrInfo.oldViewConY == this.baseScrInfo.tempY) this.baseScrInfo.eqkind = 1;
                    else {
                        this.baseScrInfo.oldViewConY--;
                        this.baseScrInfo.eqkind = 0;
                    }
                    this.checkItemValue(this.baseScrInfo.eqkind);
                }
            };
            e.prototype.setScrollNodeBaseValue = function (t, e, o, a, i) {
                if (cc.isValid(t)) {
                    t.parent = e;
                    t.width = o;
                    t.height = a;
                    t.setPosition(i);
                }
            };
            e.prototype.makeScroll = function (t, e, o) {
                var a = new cc.Node(t);
                this.setScrollNodeBaseValue(a, this.node, o.x, o.y, e);
                var i = a.addComponent(cc.ScrollView);
                a.on("scrolling", this.scrollEvent, this);
                i.brake = 0.9;
                i.horizontal = !this.dragdir_v;
                i.vertical = this.dragdir_v;
                i.cancelInnerEvents = this.isIgnoreChildenClick;
                var n = new cc.Node("mask");
                n.addComponent(cc.Mask);
                this.setScrollNodeBaseValue(n, a, o.x, o.y, cc.Vec3.ZERO);
                var r = new cc.Node("content");
                this.setScrollNodeBaseValue(r, n, o.x, 0, cc.Vec3.ZERO);
                a.color = cc.Color.BLACK;
                a.anchorX = this.dragdir_v ? 0.5 : 0;
                a.anchorY = this.dragdir_v ? 0 : 0.5;
                n.anchorX = a.anchorX;
                n.anchorY = a.anchorY;
                if (this.dragdir_v) {
                    r.x = o.x * r.anchorX;
                    r.anchorY = this.makeDir_v ? 1 : 0;
                    i.content = r;
                    this.baseScrInfo.OY = this.makeDir_v ? r.y : 0;
                    r.y = this.baseScrInfo.OY;
                } else {
                    r.anchorX = this.makeDir_v ? 1 : 0;
                    r.y = 0;
                    i.content = r;
                    this.baseScrInfo.OY = this.makeDir_v ? r.x : 0;
                    r.x = this.baseScrInfo.OY;
                }
                return r;
            };
            e.prototype.checkItemValue = function (t) {
                if (2 == t) {
                    this.baseScrInfo.TempItem =
                        this.toolsInfo.all[this.baseScrInfo.curMin % this.baseScrInfo.itemMaxCount];
                    if (!this.baseScrInfo.TempItem) return;
                    this.setItemPos(
                        this.baseScrInfo.TempItem.v,
                        this.baseScrInfo.curMin + this.baseScrInfo.itemMaxCount
                    );
                    this.baseScrInfo.TempItem.id = this.baseScrInfo.curMin + this.baseScrInfo.itemMaxCount;
                    this.baseScrInfo.curMax++;
                    this.baseScrInfo.curMin++;
                } else if (0 == t) {
                    this.baseScrInfo.TempItem =
                        this.toolsInfo.all[this.baseScrInfo.curMax % this.baseScrInfo.itemMaxCount];
                    if (!this.baseScrInfo.TempItem) return;
                    this.setItemPos(
                        this.baseScrInfo.TempItem.v,
                        this.baseScrInfo.curMax - this.baseScrInfo.itemMaxCount
                    );
                    this.baseScrInfo.TempItem.id = this.baseScrInfo.curMax - this.baseScrInfo.itemMaxCount;
                    this.baseScrInfo.curMax--;
                    this.baseScrInfo.curMin--;
                }
                this.toolsInfo.setItemAction(
                    this.baseScrInfo.TempItem.id,
                    this.baseScrInfo.TempItem.v,
                    this.baseScrInfo.TempItem.c
                );
            };
            e.prototype._basePos = function (t) {
                this.baseScrInfo.tempY = t;
                this.baseScrInfo.oldViewConY = t;
                var e = this.getOffectMoveV2(t).add(
                    cc.v2(this.baseScrInfo.viewContent.x, this.baseScrInfo.viewContent.y)
                );
                this.baseScrInfo.viewContent.setPosition(e);
                this.baseScrInfo.curMin = t;
                this.baseScrInfo.curMax = t + this.baseScrInfo.itemMaxCount - 1;
                for (var o = 0; o < this.toolsInfo.all.length - this.toolsInfo.pooler.length; o++) {
                    this.baseScrInfo.TempItem = this.toolsInfo.all[(o + t) % this.baseScrInfo.itemMaxCount];
                    this.setItemPos(this.baseScrInfo.TempItem.v, o + t);
                    this.baseScrInfo.TempItem.id = o + t;
                    this.toolsInfo.setItemAction(
                        this.baseScrInfo.TempItem.id,
                        this.baseScrInfo.TempItem.v,
                        this.baseScrInfo.TempItem.c
                    );
                }
            };
            e.prototype.getCurIndex = function () {
                return this.baseScrInfo.oldViewConY;
            };
            e.prototype.getOffectMoveV2 = function (t) {
                return this.dragdir_v
                    ? cc.v2(0, (this.makeDir_v ? 1 : -1) * (this.baseItemDis + this.spaceDis) * t)
                    : cc.v2((this.makeDir_v ? 1 : -1) * (this.baseItemDis + this.spaceDis) * t, 0);
            };
            e.prototype.setItemPos = function (t, e) {
                this.dragdir_v
                    ? (t.y =
                          (this.makeDir_v ? -1 : 1) *
                          (this.startItemOffect +
                              this.baseItemDis * t.anchorY +
                              (this.baseItemDis + this.spaceDis) * e))
                    : (t.x =
                          (this.makeDir_v ? -1 : 1) *
                          (this.startItemOffect +
                              this.baseItemDis * t.anchorX +
                              (this.baseItemDis + this.spaceDis) * e));
            };
            e.prototype.getNewItem = function () {
                if (this.toolsInfo.pooler.length > 0) {
                    var t = this.toolsInfo.pooler.pop();
                    t.v.active = !0;
                    return t;
                }
                var e = null;
                (e = this.getNodeList ? this.getNodeList() : cc.instantiate(this.basePreFab)).parent =
                    this.getViewContent();
                this.curPreItemCount++;
                var o = {id: this.toolsInfo.all.length, v: e, c: {}};
                this.toolsInfo.all.push(o);
                return o;
            };
            e.prototype.getViewContent = function () {
                return this.baseScrInfo.viewContent;
            };
            e.prototype.recAll = function () {
                var t = this,
                    e = this.getViewContent();
                e && this.oldRemberPos && e.setPosition(this.oldRemberPos);
                this.toolsInfo.all.forEach(function (e) {
                    t.recItem(e);
                });
            };
            e.prototype.recItem = function (t) {
                if (t.v.active) {
                    t.v.active = !1;
                    this.toolsInfo.pooler.push(t);
                }
            };
            e.prototype.checKIsEnterMapSelect = function () {
                return !(!this.basePreFab && !this.getNodeList);
            };
            e.prototype.updateAllItem = function () {
                for (var t = 0; t < this.toolsInfo.all.length; t++) {
                    this.baseScrInfo.TempItem = this.toolsInfo.all[t];
                    this.toolsInfo.setItemAction(
                        this.baseScrInfo.TempItem.id,
                        this.baseScrInfo.TempItem.v,
                        this.baseScrInfo.TempItem.c
                    );
                }
            };
            e.prototype.clearQLooperNode = function () {
                if (this.putNodeList) {
                    for (var t = 0; t < this.toolsInfo.all.length; t++) this.putNodeList(this.toolsInfo.all[t].v);
                    this.toolsInfo.all.length = 0;
                    for (t = 0; t < this.toolsInfo.pooler.length; t++) this.putNodeList(this.toolsInfo.pooler[t].v);
                    this.toolsInfo.pooler.length = 0;
                }
            };
            e.prototype.setPutGetNode = function (t, e) {
                this.getNodeList = t;
                this.putNodeList = e;
            };
            n([c({type: cc.Prefab, displayName: "单个组的预制体", tooltip: ""})], e.prototype, "basePreFab", void 0);
            n([c({displayName: "是否纵向拖动", tooltip: "true为开启 false为关闭"})], e.prototype, "dragdir_v", void 0);
            n(
                [
                    c({
                        displayName: "是否从正向开始生成",
                        tooltip: "纵向 true 上往下 false下往上,横向 true 左到右 false 右到左"
                    })
                ],
                e.prototype,
                "makeDir_v",
                void 0
            );
            n(
                [c({displayName: "屏蔽子节点点击", tooltip: "true 子组点击不响应 flase 点击子组会响应"})],
                e.prototype,
                "isIgnoreChildenClick",
                void 0
            );
            n(
                [c({type: cc.Integer, displayName: "单组距离", tooltip: "纵向单个子组高度，横向为子组的宽度"})],
                e.prototype,
                "baseItemDis",
                void 0
            );
            n(
                [c({type: cc.Integer, displayName: "开始的偏移", tooltip: "距离起始点的位置"})],
                e.prototype,
                "startItemOffect",
                void 0
            );
            n(
                [c({type: cc.Integer, displayName: "组高间隔", tooltip: "单个组额外间隔"})],
                e.prototype,
                "spaceDis",
                void 0
            );
            return n([s], e);
        })(cc.Component));
o.default = p;
